
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;

using System.Web.UI;
using System.Web.UI.WebControls;

using Swaf.Gui;
using Swaf.Gui.AspNet2;

namespace Syrinx.Gui.AspNet
{
    public class MultiActionLinkButton : Swaf.Gui.AspNet2.UserControl
    {
        protected string m_txt = "";
        protected string m_imageOnUrl = "img/next_on.gif";
        protected bool m_isLast = false;
        protected LinkButton m_linkActionButton;
        protected ImageButton m_imageActionButton;
        protected System.Web.UI.WebControls.Label m_post;
        protected IList m_actions = new ArrayList();
		protected bool m_hideOnDisabled = true;
        //protected string m_templateName = "MultiActionLinkButton.ascx";

        protected override void  OnLoad(EventArgs e)
        {
	        base.OnLoad(e);
            setupControls();
            if(m_linkActionButton!=null)
			    m_linkActionButton.Click += new EventHandler(ActionButton_Click);
            if (m_imageActionButton != null)
                m_imageActionButton.Click += new ImageClickEventHandler(m_imageActionButton_Click);

		}

       
        protected void setupControls()
        {
            createControls();
            m_post = (System.Web.UI.WebControls.Label)FindControl("post");
            m_linkActionButton = FindControl("btn")as LinkButton;
            if (m_linkActionButton != null)
                m_linkActionButton.Text = m_txt;
            m_imageActionButton = FindControl("btn") as ImageButton;
            if (m_imageActionButton != null)
                m_imageActionButton.ImageUrl = m_txt;
            

            if (m_post != null && m_isLast)
                m_post.Visible = false;
        }

		/*
        public override string ContentName
        {
            get
            {
                return this.GetType().Namespace + "." + TemplateName ;
            }
        }
		 */
        public string ImageOnURL
        {
            get { return m_imageOnUrl; }
            set { m_imageOnUrl = value; }
        }
		/*
        public string TemplateName
        {
            get { return m_templateName; }
            set { m_templateName = value; }
        }
		 */
        public bool isLast
        {
            set
            {
                m_isLast = value;
                //setupControls();
            }
        }

        public string Text
        {
            get
            {
                setupControls();
                if(m_linkActionButton != null)
                    return m_linkActionButton.Text;
                return m_txt;
            }
            set
            {
                m_txt = value;
                //setupControls();
            }
        }

		public bool HideOnDisabled
		{
			get { return m_hideOnDisabled; }
			set { m_hideOnDisabled = value; }
		}

        void m_imageActionButton_Click(object sender, ImageClickEventArgs e)
        {
            m_imageActionButton.ImageUrl = ImageOnURL;
            triggerAction();
        }
		void ActionButton_Click(object sender, EventArgs e)
		{
            triggerAction();
		}

        protected virtual void triggerAction()
        {
            foreach (IAction action in m_actions)
                if (action.Enabled)
                    action.trigger();
        }

        public void addAction(IAction action)
        {
            m_actions.Add(action);
			action.ActionChanged += new ActionChanged(m_action_ActionChanged);
            setupStateFromActions();
		}

        protected void setupStateFromActions()
        {
            setupControls();
            bool enabled = false;
            foreach(IAction action in m_actions)
                if (action.Enabled == true)
                {
                    enabled = true;
                    break;
                }
            if(m_linkActionButton!=null)
                m_linkActionButton.Enabled = enabled;
            if (m_imageActionButton != null)
                m_imageActionButton.Enabled = enabled;

            if (!enabled)
                this.Visible = !m_hideOnDisabled;
            else
                this.Visible = true;
        }

		void m_action_ActionChanged(object sender, ActionChangeType type)
		{
            setupStateFromActions();
		}

    }
}
